local Version = 1.5;
local Author = "Avree"


function ShutUp_OnLoad(frame)
	if AddonManager then AddonManager.RegisterAddon("Shut Up!", "Blacklisting by keyword for spammers of all shapes and sizes.","Interface/Addons/ShutUp/shutup.tga","Social",nil,"/su")
	end
	frame:RegisterEvent("CHAT_MSG_ZONE");
	frame:RegisterEvent("CHAT_MSG_SAY");
	frame:RegisterEvent("CHAT_MSG_WHISPER");
	frame:RegisterEvent("CHAT_MSG_YELL");
	SLASH_SU_SlashHandler1 = "/su";
	SlashCmdList["SU_SlashHandler"] = SU_SlashHandler;
	DEFAULT_CHAT_FRAME:AddMessage("Shut Up! Online. Type /su help for commands.",0,255,255);
end;

defaultPatterns = {"gamekoo", "mmogood", "3zoom", "gaincoin", "8thgame", "gamecuu", "leveln.de"}
	if not ShutUp_patterns
		then
			ShutUp_patterns = defaultPatterns
		 end
		SaveVariables("ShutUp_patterns")

function ShutUp_OnEvent(frame, event) 
	if string.match(event, "^CHAT_MSG")
		then 
	local s = string.lower(arg1);
	local s = string.gsub(s," ","");
	for index,pattern in ipairs(ShutUp_patterns)
	do if string.match(s, pattern)
		then 	if ShutUp_patterns["show"] then arg1=("|cff333333" .. arg1) GetBadFriendNames(fName); AddFriend("BadFriend",arg4);
			else arg1="|cff333333 Spammer has been shut up." GetBadFriendNames(fName); AddFriend("BadFriend",arg4);
		end;
		end; 
	end;
end;
end

function GetBadFriendNames(fName)
    index = 0;
    y1 = GetFriendCount("BadFriend");
	if y1 > 20 then
		for i = 1, y1, 1 do
		local name, _, _, _ ,_, _, _, _, _, _ = GetFriendInfo("BadFriend", i );
		DelFriend("BadFriend",name);
		DEFAULT_CHAT_FRAME:AddMessage("ShutUp: "..name.." has been removed from Blacklist in order to prevent overflow.",255,0,0);
	end
	else
	        index = index+1;
    end
end

function SU_SlashHandler(editBox, msg)
	if msg == "" then
		SU_NoMSG();
	end
	msg = string.lower(msg);
	if (msg:match("^add")) then
		SU_Add(msg:match("^add%s+(.*)"));
	elseif (msg:match("^remove")) then
		SU_Remove(msg:match("^remove%s+(.*)"));
	elseif (msg == "help") then
		SU_NoMSG();
	elseif (msg:match("^list")) then
		SU_Print();
	elseif (msg:match("^show")) then
		SU_Show();
	elseif (msg:match("^hide")) then
		SU_Hide();
	end
end

function SU_NoMSG()
	SUMsg("Invalid command, type:");
	SUMsg("/su add <word> - to add a spam-filtered word (remove brackets)");
	SUMsg("/su remove <word> - to remove a spam-filtered word (remove brackets)");
	SUMsg("/su list - lists currently spam-filtered words.");
	SUMsg("/su show - sets Shut Up! to show spam messages as it blacklists spammers.");
	SUMsg("/su hide - sets Shut Up! to replace spam messages as it blacklists spammers.");
	SUMsg("/su help - to show this message");
end

function SUMsg(msg,red,green,blue)
	if ((red) and (green) and (b)) then
		DEFAULT_CHAT_FRAME:AddMessage(msg, red, green, blue);
	else
		DEFAULT_CHAT_FRAME:AddMessage(msg);
	end
end

function SU_Add(pattern)
	  ShutUp_patterns[#ShutUp_patterns+1] = pattern
	  SUMsg("Word has been added.")
	SaveVariables("ShutUp_patterns")
end  

function SU_Remove(pattern)
	 for i,p in ipairs(ShutUp_patterns) do
		 if p == pattern then table.remove (ShutUp_patterns,i) end
	 end
	 SUMsg("Word has been removed.")
			SaveVariables("ShutUp_patterns")
end

function SU_Print()
	DEFAULT_CHAT_FRAME:AddMessage("Shut Up! is filtering the following phrases:")
		for i,p in ipairs(ShutUp_patterns) do
	DEFAULT_CHAT_FRAME:AddMessage(p)
	end
end

function SU_Show()
	DEFAULT_CHAT_FRAME:AddMessage("Shut Up! will display spammers' messages.")
		ShutUp_patterns["show"] = true
		SaveVariables("ShutUp_patterns")
end

function SU_Hide()
	DEFAULT_CHAT_FRAME:AddMessage("Shut Up! will hide spammers' messages.")
		ShutUp_patterns["show"] = nil
		SaveVariables("ShutUp_patterns")
end

